package com.ruifu.doc.controller;

import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.vo.ResultVO;
import com.ruifu.doc.dto.foldcont.DmFoldContDeleteDTO;
import com.ruifu.doc.dto.foldcont.DmFoldContInsertDTO;
import com.ruifu.doc.service.DmFoldContService;
import com.ruifu.doc.vo.foldcont.DmFoldContVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文件夹内容控制层
 *
 * @author nyr
 * @create 2019/11/11
 */
@RestController
@CrossOrigin
@RequestMapping("/dmFoldCont")
public class DmFoldContController {
    @Autowired
    private DmFoldContService dmFoldContService;

    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }

    /**
     * 批量添加文档到文件夹
     *
     * @param dmFoldContInsertDTO
     * @return
     */
    @PostMapping("/insertPL")
    public ResultVO insertPL(@RequestBody DmFoldContInsertDTO dmFoldContInsertDTO) {
        try {
            dmFoldContService.insertPL(dmFoldContInsertDTO, cloudUser);
            return new ResultVO(true, "添加文件夹内容成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 批量移除文件夹内容
     *
     * @param dmFoldContDeleteDTO 文件夹删除内容DTO
     * @return
     */
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody DmFoldContDeleteDTO dmFoldContDeleteDTO) {
        try {
            dmFoldContService.batchDelete(dmFoldContDeleteDTO, cloudUser);
            return new ResultVO(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 根据文件夹id查询文件夹内容
     *
     * @param fcFoldId    文件夹id
     * @param fcObjTypeId 文件夹内容类型id
     * @return
     */
    @GetMapping("/findListByFcFoldId/{fcFoldId}/{fcObjTypeId}")
    public List<DmFoldContVO> findListByFcFoldId(@PathVariable Long fcFoldId, @PathVariable Long fcObjTypeId) {
        return dmFoldContService.findListByFcFoldId(fcFoldId, fcObjTypeId, cloudUser);
    }
}
